package hot100;


/*
 * Author：江松
 * Date：2023/4/16 9:47
 *
 合并2个有序链表：
辅助头
 */

public class Main21 {
    public class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }


    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode head=new ListNode();
        ListNode pre=head;
        while(list1!=null&&list2!=null){
            if(list1.val<=list2.val){
                pre.next=list1;
                list1=list1.next;
            }else{
                pre.next=list2;
                list2=list2.next;
            }
            pre=pre.next;
            System.out.println(pre.val);
        }
        pre.next=list1!=null?list1:list2;
        return head.next;
    }
}
